package com.sign2024.sign2024project.test;

import com.alibaba.excel.EasyExcel;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024project.projectservice.mapper.StoreAreaMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 本批需求一览表 控制层
 *
 * @author yujie
 * @data 2024/7/25 15:50
 */
@Api(tags = "排查")
@RestController
@RequestMapping("/paicha")
public class StoreAreaController {

    @Resource
    private StoreAreaMapper storeAreaMapper;

    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @Transactional
    @PostMapping("/import")
    public CommonResult importInfo(MultipartFile file) {
        try {
            List<Box> objects = EasyExcel.read(file.getInputStream()).head(Box.class).sheet().doReadSync();
            System.out.println(objects.size());
            List<String> list = new ArrayList<>();
            int index = 0;
            for (Box b : objects) {
                if (b.getBoxMark() != null) {
                    index += 1;
                    System.out.println("标记：" + index + " : " + b.getBoxMark());
                    list.add(b.getBoxMark());
                }
            }

            System.out.println(list);

            List<String> boxList = storeAreaMapper.selectStoreAreaListByPrefix2("LA_SHELF_STORAGE_H");
            System.out.println(boxList.size());

            // 找出在 boxList 中但不在 list 中的元素
            List<String> onlyInBoxList = new ArrayList<>();
//            for (String item : list) {
//                if (!boxList.contains(item)) {
//                    onlyInBoxList.add(item);
//                }
//            }

            for (String item : boxList) {
                if (!list.contains(item)) {
                    onlyInBoxList.add(item);
                }
            }

            onlyInBoxList.forEach(System.out::println);

            return CommonResult.success(onlyInBoxList);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @PostMapping("/message")
    public void message() {
        myWebSocketHandler.sendMessageToLocation("user123", "注意接受消息");
    }
}
